from selenium import webdriver
import time
import DBUtils


class TestResigister:

    def test_register1(self):

        driver = webdriver.Chrome()

        driver.get(url="http://localhost:90/")

        driver.maximize_window()

        driver.find_element_by_xpath('//div[@class="login-center1"]/div[2]/a').click()

        # 注册页面链接
        time.sleep(2)

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("wangwu")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")

        driver.find_element_by_xpath('//input[@id="repassword"]').send_keys('123456')

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        time.sleep(2)

        cur_url = driver.current_url
        # http://localhost:90/

        sql = 'select * from user where username = %s'
        params = ['wangwu']
        data = DBUtils.select(sql,params)

        if cur_url == 'http://localhost:90/' and len(data) == 1:
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot(filename='wangwu-123456-123456.png')

        assert cur_url == 'http://localhost:90/' and len(data) == 1

        driver.quit()

        sql = "delete from user where username = %s"
        params = ['wangwu']

        DBUtils.update(sql,params)


    def test_register2(self):

        sql = 'insert into user(username,password) values(%s,md5(%s))'
        params = ['wangwu1','123456']

        DBUtils.update(sql,params)
        # 先在数据库中插user表中插入一条username wangwu1 password 123456 的信息

        driver = webdriver.Chrome()

        driver.get(url="http://localhost:90/")

        driver.maximize_window()

        driver.find_element_by_xpath('//div[@class="login-center1"]/div[2]/a').click()

        # 注册页面链接
        time.sleep(2)

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("wangwu1")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")

        driver.find_element_by_xpath('//input[@id="repassword"]').send_keys('123456')

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        time.sleep(2)

        # cur_url = driver.current_url
        # http://localhost:90/

        sql = 'select * from user where username = %s'
        params = ['wangwu1']
        data = DBUtils.select(sql,params)

        if len(data) == 1:
            print('通过！')
        else:
            print('不通过')
            driver.save_screenshot('test_register2.png')

        sql = "delete from user where username = %s"
        params = ['wangwu1']

        DBUtils.update(sql, params)

        driver.quit()

        assert len(data) == 1


    def test_register3(self):

        # //div[@class='login-center1']/form/div[3]/span[1]

        driver = webdriver.Chrome()

        driver.get(url="http://localhost:90/")

        driver.maximize_window()

        driver.find_element_by_xpath('//div[@class="login-center1"]/div[2]/a').click()

        # 注册页面链接
        time.sleep(2)

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("wangwu")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")

        driver.find_element_by_xpath('//input[@id="repassword"]').send_keys('1234567')

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()


        try:
            result = driver.find_element_by_xpath("//div[@class='login-center1']/form/div[3]/span[1]").text

            time.sleep(1)

        except Exception as e:
            print(e)

        finally:

            if result == '两次密码不相同':

                print('通过')
            else:
                print('不通过')
                driver.save_screenshot('test_register3.png')

            driver.quit()
            assert result == '两次密码不相同'

    def test_register4(self):

        sql = "delete from user where username = %s"
        params = ['wangwu']

        DBUtils.update(sql, params)

        # //div[@class='login-center1']/form/div[3]/span[1]

        driver = webdriver.Chrome()

        driver.get(url="http://localhost:90/")

        driver.maximize_window()

        driver.find_element_by_xpath('//div[@class="login-center1"]/div[2]/a').click()

        # 注册页面链接
        time.sleep(2)

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("wangwu")

        driver.find_element_by_xpath('//input[@id="password"]').send_keys("123456")

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        driver.implicitly_wait(5)

        result = driver.find_element_by_xpath("//div[@class='login-center1']/form/div[3]/span[1]").text

        if result == '重复密码为空':
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot('test_register4.png')

        driver.quit()

        assert result == '重复密码为空'


    def test_register5(self):
        # //div[@class='login-center1']/form/div[3]/span[1]

        driver = webdriver.Chrome()

        driver.get(url="http://localhost:90/")

        driver.maximize_window()

        driver.find_element_by_xpath('//div[@class="login-center1"]/div[2]/a').click()

        # 注册页面链接
        time.sleep(2)

        driver.find_element_by_xpath('//input[@id="username"]').send_keys("wangwu")

        driver.find_element_by_xpath('//button[@id="login_btn"]').click()

        time.sleep(1)

        result = driver.find_element_by_xpath("//div[@class='login-center1']/form/div[2]/span[1]").text

        if result == '注册密码不能为空':
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot('test_register5.png')

        driver.quit()

        assert result == '注册密码不能为空'




